package com.tools.web.swagger;

import com.tools.common.object.Note;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.Contact;

import java.util.Objects;

/**
 * Swagger API 构建对象
 * */
@Note("Swagger API 构建对象")
public class SwaggerAPI {

    @Note("API 文档的标题")
    private String title;

    @Note("API 文档简短的描述信息")
    private String description;

    @Note("作者的姓名、源地址、联系邮箱")
    private Contact contact;

    @Note("当前 API 文档的版本")
    private String version;


    /* ***********************************************************************************
     *
     *         构造器
     *
     * **********************************************************************************
     * */

    public SwaggerAPI() {
        this(null, null, null, null);
    }

    public SwaggerAPI(String title) {
        this(title, null, null, null);
    }

    public SwaggerAPI(String title, Contact contact) {
        this(title, null, contact, null);
    }

    public SwaggerAPI(String title, String version) {
        this(title, null, null, version);
    }

    public SwaggerAPI(String title, Contact contact, String version) {
        this(title, null, contact, version);
    }

    public SwaggerAPI(String title, String description, Contact contact, String version) {
        this.setTitle(title);
        this.setDescription(description);
        this.setContact(contact);
        this.setVersion(version);
    }

    public static SwaggerAPI of() {
        return new SwaggerAPI();
    }

    public static SwaggerAPI of(String title) {
        return new SwaggerAPI(title);
    }

    public static SwaggerAPI of(String title, Contact contact) {
        return new SwaggerAPI(title, contact);
    }

    public static SwaggerAPI of(String title, String version) {
        return new SwaggerAPI(title, version);
    }

    public static SwaggerAPI of(String title, Contact contact, String version) {
        return new SwaggerAPI(title, contact, version);
    }

    public static SwaggerAPI of(String title, String description, Contact contact, String version) {
        return new SwaggerAPI(title, description, contact, version);
    }


    /* ***********************************************************************************
     *
     *         Getter / Setter
     *
     * **********************************************************************************
     * */

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = (title != null && !title.isEmpty()) ? title : "Swagger API 文档";
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Contact getContact() {
        return contact;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }

    public void setContact(String authorName, String url, String email) {
        Contact contact = new Contact();
        contact.setName(authorName);
        contact.setUrl(url);
        contact.setEmail(email);
        this.contact = contact;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = (version != null && !version.isEmpty()) ? version : "v0.0.1";
    }

    @Override
    public String toString() {
        return "SwaggerAPI{" +
                "title=" + title +
                ", version=" + version +
                ", description=" + description +
                ", contact={authorName=" + contact.getName() + ", url=" + contact.getUrl() + ", email=" + contact.getEmail() + '}' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SwaggerAPI that = (SwaggerAPI) o;
        if(!Objects.equals(title, that.title) || !Objects.equals(version, that.version)) {
            return false;
        }
        return contact == that.contact ||
                (Objects.equals(contact.getName(), that.contact.getName())
                        && Objects.equals(contact.getUrl(), that.contact.getUrl())
                        && Objects.equals(contact.getEmail(), that.contact.getEmail())
                );
    }

    @Override
    public int hashCode() {
        return Objects.hash(title, version, contact.getName(), contact.getUrl(), contact.getEmail());
    }

    /* ***********************************************************************************
     *
     *         链式调用
     *
     * **********************************************************************************
     * */

    public SwaggerAPI title(String title) {
        this.setTitle(title);
        return this;
    }

    public SwaggerAPI description(String description) {
        this.setDescription(description);
        return this;
    }

    public SwaggerAPI contact(Contact contact) {
        this.setContact(contact);
        return this;
    }

    public SwaggerAPI contact(String authorName, String url, String email) {
        this.setContact(authorName, url, email);
        return this;
    }

    public SwaggerAPI version(String version) {
        this.setVersion(version);
        return this;
    }

    /* ***********************************************************************************
     *
     *         功能方法
     *
     * **********************************************************************************
     * */


    public Info toApiInfo() {
        return new Info()
                .title(this.title)
                .description(this.description)
                .contact(this.contact)
                .version(this.version);
    }
}
